package com.wjj.happytime.service;

import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.annotation.Nullable;
import android.widget.RemoteViews;
import com.wjj.happytime.R;
import com.wjj.happytime.activity.listen_activity.PlayActivity;
import java.io.IOException;

/**
 * Created by junjie.wang on 2017/4/25.
 */

public class PlayService extends Service implements OnPreparedListener {

  public MediaPlayer mediaPlayer;
  public WifiManager.WifiLock lock = null;

  //事件类型
  public static final String ACTION_PLAY = "com.play.ACTION_PLAY";
  public static final String ACTION_PAUSE = "com.play.ACTION_PAUSE";
  public static final String ACTION_EXIT = "com.play.ACTION_EXIT";
  public String radioName;
  public Builder builder;

  public PlayService() {
  }

  @Override
  public void onCreate() {
    super.onCreate();
    mediaPlayer = new MediaPlayer();
    //后台保持 cpu 正常的工作
    mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

    //后台保持 wifi 不被休眠
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    lock = wifiManager.createWifiLock("mylock");
    lock.acquire();

    mediaPlayer.setOnPreparedListener(this);



  }

  private void notification() {
    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, PlayActivity.class),
        PendingIntent.FLAG_UPDATE_CURRENT);
    builder = new Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(radioName)
        .setContentText("王俊杰")
        .setContentIntent(pi)
        .setContent(new RemoteViews(getPackageName(),R.layout.remote_view_layout));

    //发送通知
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = builder.build();
    //作为前台的服务，退出时不会被系统给杀掉
    startForeground(0, notification);
    nm.notify(0, notification);
  }



  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    String action = intent.getAction();
    String playUrl = intent.getStringExtra("playUrl");
    radioName = intent.getStringExtra("radioName");
    //在通知栏显示
    notification();
    if (ACTION_PLAY.equals(action)) {
      mediaPlayer.reset();
      try {
        mediaPlayer.setDataSource(playUrl);
        mediaPlayer.prepareAsync();

      } catch (IOException e) {
        e.printStackTrace();
      }


    } else if (ACTION_PAUSE.equals(action)) {

      if (mediaPlayer.isPlaying()) {
        mediaPlayer.pause();
      }

    } else if (ACTION_EXIT.equals(action)) {
      if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
        mediaPlayer.release();
      }

    }

    return super.onStartCommand(intent, flags, startId);
  }

  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

  @Override
  public void onPrepared(MediaPlayer mediaPlayer) {
    mediaPlayer.start();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    //释放锁 当暂停或停止时，要释放锁
    lock.release();
    stopForeground(true);//退出时，删除
  }
}
